//
//  TogetherRootViewController.m
//  Together_iOS
//
//  Created by QIU Shuang on 3/1/14.
//  Copyright (c) 2014 QIU Shuang. All rights reserved.
//

#import "TogetherRootViewController.h"
#import "TogetherBaiduMapViewController.h"
#import "TogetherDateArrangerViewController.h"

#import "TogetherAppDelegate.h"

#import "TogetherToolbar.h"
#import "NSDate+Helper.h"

@interface TogetherRootViewController () <UIAlertViewDelegate>

@property (strong, nonatomic) UIButton *coupleButton;
@property (strong, nonatomic) UIButton *groupButton;
@property (nonatomic, strong) UITextField *textField;

@end

@implementation TogetherRootViewController

@synthesize coupleButton = _coupleButton;
@synthesize groupButton = _groupButton;

@synthesize delegate = _delegate;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void) loadView {
    [super loadView];
    
    // initialize couple button
    self.coupleButton = [[UIButton alloc] initWithFrame:CGRectMake(88, 110, 144, 167)];
    [self.coupleButton setImage:[UIImage imageNamed:@"button_couple.png"] forState:UIControlStateNormal];
    [self.coupleButton addTarget:self action:@selector(startMakingPlan:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.coupleButton];
    
    // initialize group button
    self.groupButton = [[UIButton alloc] initWithFrame:CGRectMake(88, 289, 144, 167)];
    [self.groupButton setImage:[UIImage imageNamed:@"button_group.png"] forState:UIControlStateNormal];
//    [self.groupButton addTarget:self action:@selector(startMakingPlan:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.groupButton];
    
    UIImageView *teamLogo = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"teamname.png"]];
    teamLogo.transform = CGAffineTransformMakeScale(0.6, 0.6);
    teamLogo.frame = CGRectOffset(teamLogo.frame, 63, 455);
    [self.view addSubview:teamLogo];
    
    // set up TabBarViewController
    self.title = @"Together";
    // change navigation item bar
    [self.navigationItem setBackBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil]];
    
    TogetherToolbar *toolbar = [[TogetherToolbar alloc] initWithFrame:CGRectMake(0, 524, 320, 44)];    toolbar.delegate = self;
    [self.view addSubview:toolbar];
    
    
    _textField = [[UITextField alloc] initWithFrame:CGRectMake(-100., -100., 10., 10.)];
    _textField.hidden = NO;
    [self.view addSubview:_textField];
}


- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    UIDatePicker *datePicker = [[UIDatePicker alloc] init];
    datePicker.datePickerMode = UIDatePickerModeDate;
    datePicker.minimumDate = [NSDate dateWithTimeIntervalSinceNow:0];
    datePicker.maximumDate = [NSDate dateWithTimeIntervalSinceNow:3600*24*10];
    _textField.inputView = datePicker;
    
    // create a done view + done button, attach to it a doneClicked action, and place it in a toolbar as an accessory input view...
    // Prepare done button
    UIToolbar* keyboardDoneButtonView = [[UIToolbar alloc] init];
    keyboardDoneButtonView.barStyle = UIBarStyleBlack;
    keyboardDoneButtonView.translucent = YES;
    keyboardDoneButtonView.tintColor = nil;
    [keyboardDoneButtonView sizeToFit];
    
    UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:@"完成"
                                                                   style:UIBarButtonItemStyleDone
                                                                  target:self
                                                                  action:@selector(doneClicked:)];
    UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"取消"
                                                                     style:UIBarButtonItemStylePlain
                                                                    target:self
                                                                    action:@selector(cancelClicked:)];

    [keyboardDoneButtonView setItems:[NSArray arrayWithObjects:doneButton, cancelButton, nil]];
    
    // Plug the keyboardDoneButtonView into the text field...
    _textField.inputAccessoryView = keyboardDoneButtonView;
    
    UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithTitle:@"IP" style:UIBarButtonItemStylePlain target:self action:@selector(selectIP:)];
    self.navigationItem.rightBarButtonItem = rightBarButton;

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void) reserve:(id) sender {
    UIButton *source = (UIButton *)sender;
    NSLog(@"event source %@", [source currentTitle]);
    [self.delegate willShowViewController:[[TogetherBaiduMapViewController alloc] init] animated:YES];
}


- (void)startMakingPlan:(id)sender {
    [_textField becomeFirstResponder];
}

- (void)setDateClicked:(id)sender {
    [_textField becomeFirstResponder];
}

- (void)doneClicked:(id)sender {
    // Write out the date...
    [_textField resignFirstResponder];
    [self showDateArranger:nil];
}

- (void)cancelClicked:(id)sender {
    [_textField resignFirstResponder];
}


- (void)showDateArranger:(id)sender {
    TogetherDateArrangerViewController *arrangerViewController = [[TogetherDateArrangerViewController alloc] init];
    [self.delegate willShowViewController:arrangerViewController animated:YES];
}

- (void) myTrack {
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg_myprofile.jpg"]];
    imageView.frame = CGRectMake(0, 65, 320, 503);
    UIViewController *controller = [[UIViewController alloc] initWithNibName:nil bundle:nil];
    controller.navigationItem.leftBarButtonItem.title = @"返回";
    [controller.view addSubview:imageView];
    
    TogetherToolbar *toolbar = [[TogetherToolbar alloc] initWithFrame:CGRectMake(0, 524, 320, 44)];
    [toolbar rightButtonClicked];
    [controller.view addSubview:toolbar];
    
    [self.navigationController pushViewController:controller animated:YES];
}

- (void) hotTracks {
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg_recommendation.jpg"]];
    imageView.frame = CGRectMake(0, 65, 320, 503);
    UIViewController *controller = [[UIViewController alloc] initWithNibName:nil bundle:nil];
    controller.navigationItem.leftBarButtonItem.title = @"返回";
    [controller.view addSubview:imageView];
    
    TogetherToolbar *toolbar = [[TogetherToolbar alloc] initWithFrame:CGRectMake(0, 524, 320, 44)];
    [toolbar leftButtonClicked];
    [controller.view addSubview:toolbar];
    
    [self.navigationController pushViewController:controller animated:YES];
}

- (void)back {
    [((TogetherAppDelegate *)self.delegate).navigator popViewControllerAnimated:YES];
}


- (void)selectIP:(id)sender {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"IP" message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
    alert.alertViewStyle = UIAlertViewStylePlainTextInput;
    [alert show];
}


#pragma mark - UIAlertViewDelegate 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {
        // OK button clicked.
        NSString *ip = [alertView textFieldAtIndex:0].text;
        [[NSUserDefaults standardUserDefaults] setObject:ip forKey:@"serverAddress"];
    }
}

@end
